package repo

import (
	"ginchat/models"
	"ginchat/utils"
)

func GetUserList() []*models.UserBasic {
	var data []*models.UserBasic
	err := utils.MySqlDB.Find(&data).Error
	if err != nil {
		return nil
	}
	return data
}

func CreateUser(user *models.UserBasic) error {
	err := utils.MySqlDB.Create(user).Error
	return err
}

func GetUserByID(id int64) *models.UserBasic {
	var data models.UserBasic
	err := utils.MySqlDB.Where("id = ?", id).First(&data).Error
	if err != nil {
		return nil
	}
	return &data
}

func GetUserByUserName(userName string) *models.UserBasic {
	var data models.UserBasic
	err := utils.MySqlDB.Where("user_name = ?", userName).First(&data).Error
	if err != nil {
		return nil
	}
	return &data
}

// DeleteUser 删除用户
func DeleteUser(id int64) error {
	return utils.MySqlDB.Delete(&models.UserBasic{}, id).Error
}

func UpdateUser(user *models.UserBasic) error {
	return utils.MySqlDB.Save(user).Error
}
